from configparser import ConfigParser

#写
cfg = ConfigParser()
cfg.add_section('cluster')
cfg.set('cluster', 'atom_num', '5')
cfg.set('cluster', 'element', 'H')
cfg.add_section('abinitio')
cfg.set('abinitio','method','dmol3')
#cfg.remove_option('abinitio')
f = open('config.ini', 'w')
cfg.write(f)
f.close()

#读
cfg2 = ConfigParser()
cfg2.read('config.ini')
print('sections:',cfg2.sections())
print('options of cluster:',cfg2.options('cluster'))
print('items:',cfg2.items('cluster'))
section = cfg2['cluster']
print('keys:', list(section.keys()))
print('method=',cfg2.get('abinitio','method'))
print('atom_num=',cfg2.getint('cluster','atom_num')) #getfloat, getboolean
print(cfg2.has_section('cluster'))
print(cfg2.has_option('cluster','iso_tol'))

#改
cfg2.set('cluster', 'atom_num','10')
f2 = open('modify.ini', 'w')
cfg2.write(f2)